package com.yuanduo_app.widget;

import android.util.Log;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import cn.ydxlm.vrlibrary.vr.VRPlayerView;
import com.xujl.fastlib.utils.LogS;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 地图控件的RN封装
 */
public class VRPlayerViewManager extends SimpleViewManager<VRPlayerView> {
    protected static final String TAG = "VRPlayerViewManager";

    private static final int SET_URL = 1;
    private static final int ON_RESUME = 3;
    private static final int ON_DESTROY = 4;

    private VRPlayerView mPlayerView;

    @NonNull
    @Override
    public String getName () {
        return "VRPlayerView";
    }


    @NonNull
    @Override
    protected VRPlayerView createViewInstance (@NonNull ThemedReactContext reactContext) {
        mPlayerView = new VRPlayerView(reactContext);
        return mPlayerView;
    }

    @Nullable
    @Override
    public Map<String, Integer> getCommandsMap () {
        Map<String, Integer> map = new HashMap<>();
        map.put("setUrl", SET_URL);
        map.put("onDestroy", ON_DESTROY);
        map.put("onResume", ON_RESUME);
        return map;
    }

    @Override
    public void receiveCommand (@NonNull VRPlayerView root, int commandId, @Nullable ReadableArray args) {
        super.receiveCommand(root, commandId, args);
        Log.i(TAG, "receiveCommand:" + commandId);
        switch (commandId) {
            case ON_DESTROY:
                root.onDestroy();
                break;
            case ON_RESUME:
                root.onResume();
                break;
            case SET_URL:
                root.onResume();
                if (args == null) {
                    return;
                }
                final ArrayList<Object> objects = args.toArrayList();
                Map<String, String> map = (Map<String, String>) objects.get(0);
                LogS.i(TAG, "url:" + map.get("url"));
                mPlayerView.setVideoPath(map.get("url"));
                break;
            default:
                break;

        }
    }
}
